import 'package:get/get.dart';

class GetxHomeController extends GetxController {
  var count = 0.obs;

  void increment() {
    count++;
  }

  var teacher = GetxTeacher();

  void conertToUpperCase() {
    teacher.name.value = teacher.name.value.toUpperCase();
    teacher.age.value += 1;
  }

  void conertToUpperCase2() {
    teacher.name.value = teacher.name.value.toUpperCase();
    teacher.age.value += 1;
    update();
  }

  ///----------------
  var student = GetxStudent(name: 'student', age: 20).obs;

  void changeStudentName() {
    student.update((val) {
      val?.name = 'student_name_change ${val.age}';
      val?.age++;
    });
  }

  /// 事件监听
  @override
  void onInit() {
    // 监听count变化,当它发生改变的时候调用
    ever(count, (callback) {
      print('count 变化了 $count');
    });

    // 监听多个值，当他们发生改变的时候调用
    everAll([count], (callback) => print('everAll 变化了2 $count'));

    // 只监听一次，当它第一次发生改变的时候调用
    once(count, (callback) => print('count 第一次变化了 $count'));

    // 防抖，指定时间内没有变化才调用
    // 用户停止操作一段时间后触发
    debounce(count, (callback) => print('count 防抖了 $count'), time: const Duration(seconds: 1));

    // 节流，指定时间内只调用一次
    // 一段时间内只触发一次
    interval(count, (callback) => print('count interval  $count'), time: const Duration(seconds: 3));

    super.onInit();
  }

}

class GetxTeacher {
  var name = 'xiandao'.obs;
  var age = 30.obs;
}

class GetxStudent {
  var name;
  var age;

  GetxStudent({this.name, this.age});
}